package com.whg.demo.controller;

import com.whg.demo.conf.AppProperties;
import com.whg.demo.conf.TestProperties;
import com.whg.demo.mapper.*;
import com.whg.demo.model.*;
import com.whg.demo.service.TestService;
import com.whg.demo.utils.ReadFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author whg
 * @create 2022/11/23 18:08
 * @description：
 */
@RestController
@RequestMapping("/test")
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private AppProperties appProperties;

    @Autowired
    private TestProperties testProperties;

    @Resource
    private StudentModelMapper studentModelMapper;

    @Resource
    private TestService testService;

    @Resource
    private DmBgBzModelMapper dmBgBzModelMapper;

    @Resource
    private DmBgJgfsModelMapper dmBgJgfsModelMapper;

    @Resource
    private DmBgZmxzModelMapper dmBgZmxzModelMapper;

    @Resource
    private DmBgJldwModelMapper dmBgJldwModelMapper;

    @Resource
    private DmBgGbdqModelMapper dmBgGbdqModelMapper;

    @Resource
    private DmBgGkModelMapper dmBgGkModelMapper;

    @Resource
    private DmBgXzqhModelMapper dmBgXzqhModelMapper;

    @Resource
    private DmBgJnhydModelMapper dmBgJnhydModelMapper;


    @PostMapping("/header")
    public Object testRequestHeader(@RequestHeader("Cookie") String cookie){
        Map<String,String> map = new HashMap<>();
        map.put("Cookie",cookie);
        return map;
    }

    @PostMapping("/user")
    public Object getUser(){
        User user = new User("whg","15797897837",new Date(),24,"88888");
        return user;
    }

    @GetMapping("/app")
    public JsonData getApp(){
        JsonData result = JsonData.buildSuccess();
        result.setData(appProperties);
        return result;
    }

    @GetMapping("/test")
    public JsonData getTest(){
        JsonData result = JsonData.buildSuccess();
        result.setData(testProperties);
        return result;
    }

    @GetMapping("/student")
    public JsonData getStudent(@RequestParam("id") int id){
        logger.info("调用接口student");
        StudentModel model = studentModelMapper.selectByPrimaryKey(new Integer(id));
        return JsonData.buildSuccess(model);
    }

    @GetMapping("/log")
    public JsonData testLog(){
        logger.debug("this is debug");
        logger.info("this is info");
        logger.warn("this is warn");
        logger.error("this is error");
        return JsonData.buildSuccess();
    }

    @GetMapping("getName")
    public JsonData getName(){
        List<StudentVO> list = testService.getName();
        return JsonData.buildSuccess(list);
    }

    @GetMapping("testUrl")
    public JsonData testUrl(){
        return JsonData.buildSuccess(testProperties.getTestUrl());
    }


    @GetMapping("bzDict")
    public JsonData bzDict(){
        List<String> list = ReadFileUtil.readFile();
        List<DmBgBzModel> modelList = new ArrayList<>();
        int count = 0;
        DmBgBzModel model = null;
        for (String item : list){
            if (count == 4){
                model.setHbmc(item);
                modelList.add(model);
                count = -1;
            }else if(count == 0){
                model = new DmBgBzModel();
                model.setHbzmDm(item);
            }else if (count == 2){
                model.setHghbszDm(item);
            }else if (count == 3){
                model.setHbywmc(item);
            }
            count++;
        }
        for (DmBgBzModel item : modelList){
            dmBgBzModelMapper.insertSelective(item);
        }
        return JsonData.buildSuccess(modelList);
    }

    @GetMapping("jgfsDict")
    public JsonData jgfsDict(){
        List<String> list = ReadFileUtil.readFile();
        List<DmBgJgfsModel> modelList = new ArrayList<>();
        int count = 0;
        DmBgJgfsModel model = null;
        for (String item : list){
            if (count == 2){
                model.setJgfsqc(item);
                modelList.add(model);
                count = -1;
            }else if(count == 0){
                model = new DmBgJgfsModel();
                model.setJgfsDm(item);
            }else if (count == 1){
                model.setJgfsmc(item);
            }
            count++;
        }
        for (DmBgJgfsModel item : modelList){
            dmBgJgfsModelMapper.insertSelective(item);
        }
        return JsonData.buildSuccess(modelList);
    }

    @GetMapping("zmxzDict")
    public JsonData zmxzDict(){
        List<String> list = ReadFileUtil.readFile();
        List<DmBgZmxzModel> modelList = new ArrayList<>();
        int count = 0;
        DmBgZmxzModel model = null;
        for (String item : list){
            if (count == 2){
                if("".endsWith(item)){
                    model.setZmxzmc(model.getZmxzjc());
                }else {
                    model.setZmxzmc(item);
                }
                modelList.add(model);
                count = -1;
            }else if(count == 0){
                model = new DmBgZmxzModel();
                model.setZmxzDm(item);
            }else if (count == 1){
                model.setZmxzjc(item);
            }
            count++;
        }
        for (DmBgZmxzModel item : modelList){
            dmBgZmxzModelMapper.insertSelective(item);
        }
        return JsonData.buildSuccess(modelList);
    }

    @GetMapping("jldwDict")
    public JsonData jldwDict(){
        List<String> list = ReadFileUtil.readFile();
        List<DmBgJldwModel> modelList = new ArrayList<>();
        int count = 0;
        DmBgJldwModel model = null;
        for (String item : list){
            if (count == 1){
                model.setJldwmc(item);
                modelList.add(model);
                count = -1;
            }else if(count == 0){
                model = new DmBgJldwModel();
                model.setJldwDm(item);
            }
            count++;
        }
        for (DmBgJldwModel item : modelList){
            dmBgJldwModelMapper.insertSelective(item);
        }
        return JsonData.buildSuccess(modelList);
    }

    @GetMapping("gbdqDict")
    public JsonData gbdqDict(){
        List<String> list = ReadFileUtil.readFile();
        List<DmBgGbdqModel> modelList = new ArrayList<>();
        int count = 0;
        DmBgGbdqModel model = null;
        for (String item : list){
            if (count == 5){
                model.setCbdsypbj(item);
                modelList.add(model);
                count = -1;
            }else if(count == 0){
                model = new DmBgGbdqModel();
                model.setHggjhdqszDm(item);
            }else if (count == 1){
                if("".equals(item)){
                    model.setGjhdqszmDm(model.getHggjhdqszDm());
                }else {
                    model.setGjhdqszmDm(item);
                }
            }else if (count == 2){
                model.setGjhdqjc(item);
            }else if (count == 3){
                model.setGjhdqywjc(item);
            }else if (count == 4){
                model.setYhptslbj(item);
            }
            count++;
        }
        for (DmBgGbdqModel item : modelList){
            dmBgGbdqModelMapper.insertSelective(item);
        }
        return JsonData.buildSuccess(modelList);
    }

    @GetMapping("gkDict")
    public JsonData gkDict(){
        List<DmGk> list = ReadFileUtil.getGkList();
        List<DmBgGkModel> gkList = new ArrayList<>();
        for(DmGk item : list){
            DmBgGkModel model = new DmBgGkModel();
            model.setGkDm(item.getPORT_CODE());
            model.setGkMc(item.getPORT_NAME_CN());
            model.setGkYwMc(item.getPORT_NAME_EN());
            model.setYgGkDm(item.getCUSTOMS_PORT_CODE());
            model.setYjGkDm(item.getECIQ_PORT_CODE());
            gkList.add(model);
        }
        dmBgGkModelMapper.insertBatch(gkList);
        return JsonData.buildSuccess(list);
    }

    @GetMapping("xzqhDict")
    public JsonData xzqhDict(){
        List<DmXzqh> list = ReadFileUtil.getXzqhList();
        List<DmBgXzqhModel> xzqhList = new ArrayList<>();
        for(DmXzqh item : list){
            DmBgXzqhModel model = new DmBgXzqhModel();
            model.setXzqhDm(item.getAD_DIV_CD());
            model.setXzqhMc(item.getAD_DIV_NM_CN());
            model.setXzqhYwMc(item.getAD_DIV_NM_EN());
            model.setLsXzqhDm(item.getDOM_REGION_CD());
            model.setLsXzqhMc(item.getDOM_REGION_NM());
            //dmBgXzqhModelMapper.insertSelective(model);
            xzqhList.add(model);
        }
        dmBgXzqhModelMapper.insertBatch(xzqhList);
        /*for (DmBgXzqhModel item : modelList){
            dmBgXzqhModelMapper.insertSelective(item);
        }*/
        return JsonData.buildSuccess(list);
    }

    @GetMapping("jnhydDict")
    public JsonData jnhydDict(){
        List<DmJnhyd> list = ReadFileUtil.getJnhydList();
        List<DmBgJnhydModel> xzqhList = new ArrayList<>();
        for(DmJnhyd item : list){
            DmBgJnhydModel model = new DmBgJnhydModel();
            model.setGndqDm(item.getDISTRICT_CODE());
            model.setGndqMc(item.getDISTRICT_NAME());
            model.setDqxzDm(item.getDISTRICT_TYPE());
            xzqhList.add(model);
        }
        dmBgJnhydModelMapper.insertBatch(xzqhList);
        return JsonData.buildSuccess(list);
    }

    @GetMapping("queryGk")
    public JsonData queryGk(@RequestParam("code") String code){
        List<DmBgGkModel> list = dmBgGkModelMapper.selectAll();
        Map<String,Object> result = new HashMap<>();
        long startTime = System.currentTimeMillis();
        Map<String,String> map = list.stream().collect(
                Collectors.toMap(DmBgGkModel::getGkDm,DmBgGkModel::getGkMc));
        result.put("耗时",(System.currentTimeMillis()-startTime));
        result.put("港口名称",map.get(code));
        return JsonData.buildSuccess(result);
    }

 }
